package Presenter;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import Models.YeastGensView;

public class YeastGensViewPresenter extends AbstractPresenter<YeastGensView> {
	final String SINGLEITEM = "yeastGensView";
	final String MULTIITEM = "yeastGensViews";
	
	public ArrayList<YeastGensView> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public YeastGensView tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public YeastGensView createItemFromJSONObject(JSONObject jsonObject){
		YeastGensView item = new YeastGensView();
		try{
			item.setGeneration(jsonObject.getInt("yGGeneration"));
			item.setYeastGenID(jsonObject.getInt("yeastGenID"));
			
			if(!jsonObject.isNull("yGBrinkID")){
				//create product if set
				BrinksPresenter presenter = new BrinksPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("yGBrinkID");
				item.setBrinkID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setBrinkID(null);
			}
			
			if(!jsonObject.isNull("yGYeastID")){
				//create product if set
				YeastViewPresenter presenter = new YeastViewPresenter();
				JSONObject jsonGroup = jsonObject.getJSONObject("yGYeastID");
				item.setYeastID(presenter.createItemFromJSONObject(jsonGroup));
			} else {
				item.setYeastID(null);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(YeastGensView item){
		try{
			JSONObject json = new JSONObject();
			json.put("yGGeneration", item.getGeneration());
			json.put("yeastGenID", item.getYeastGenID());

			if(item.getBrinkID()!=null){
				final BrinksPresenter presenter = new BrinksPresenter();
				json.put("yGBrinkID", presenter.createJSON(item.getBrinkID()));
			} else {
				json.put("yGBrinkID", JSONObject.NULL);
			}

			if(item.getYeastID()!=null){
				final YeastViewPresenter presenter = new YeastViewPresenter();
				json.put("yGYeastID", presenter.createJSON(item.getYeastID()));
			} else {
				json.put("yGYeastID", JSONObject.NULL);
			}
			
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public YeastGensView getEmptyEntity(){
		YeastGensView entity = new YeastGensView();
		return entity;
	}
}
